package in.muton.edushare.poll.sections;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.util.QuestionFactory;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollPages;
import com.edushare.common.vo.PollQuestions;

public class PollSectionResult extends EdusharePanel {
	private static final long serialVersionUID = 5170984846276447104L;
	private IModel<PollPages> pageModel;

	public PollSectionResult(String id, IModel<PollPages> model,
			final EduShareBasePage page) {
		super(id, page);
		pageModel = model;

		add(new ListView<IModel<PollQuestions>>("questionList",
				new InnerList<PollQuestions>() {
					private static final long serialVersionUID = 2641845726442145796L;

					@Override
					public List<IModel<PollQuestions>> getObject() {
						List<PollQuestions> pq = pageModel.getObject()
								.getQuestions();
						Collections.sort(pq);
						return new CayenneList<PollQuestions>(pq).getObject();
					}
				}) {
			private static final long serialVersionUID = 4412891271972518860L;

			@Override
			protected void populateItem(ListItem<IModel<PollQuestions>> arg0) {
				arg0.add(QuestionFactory.getAnswerPanel("question", arg0
						.getModelObject(), page));
			}
		});
	}

	public void nextPage() {
		pageModel = PollRespondPanel.getNextPage(pageModel);
	}

	public void prevPage() {
		pageModel = PollSection.getPageByNum(new Model<Poll>(pageModel.getObject()
				.getPoll()), pageModel.getObject().getPageNumber() - 1);
	}

	public boolean isNextPagePresent() {
		return !PollRespondPanel.isLastPage(pageModel);
	}

	public boolean isPrevPagePresent() {
		return !PollRespondPanel.isFirstPage(pageModel);
	}

}
